package com.ffcc.fitness.like.service.impl;

import com.ffcc.fitness.like.mapper.LikeMapper;
import com.ffcc.fitness.like.pojo.dto.LikeDTO;
import com.ffcc.fitness.like.pojo.entity.Like;
import com.ffcc.fitness.like.pojo.vo.LikeVO;
import com.ffcc.fitness.like.service.LikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 点赞服务实现类
 */
@Slf4j
@Service
public class LikeServiceImpl implements LikeService {
    
    @Autowired
    private LikeMapper likeMapper;
    
    @Override
    @Transactional
    public void toggleLike(LikeDTO likeDTO) {
        log.info("切换点赞状态，参数：{}", likeDTO);
        
        // 验证参数
        if (likeDTO.getUserId() == null || likeDTO.getTargetId() == null || likeDTO.getTargetType() == null) {
            throw new RuntimeException("点赞参数不能为空");
        }
        
        // 检查是否已点赞
        Like existingLike = likeMapper.selectByUserAndTarget(
            likeDTO.getUserId(), 
            likeDTO.getTargetId(), 
            likeDTO.getTargetType()
        );
        
        if (existingLike != null) {
            // 已点赞，取消点赞
            int result = likeMapper.deleteByUserAndTarget(
                likeDTO.getUserId(),
                likeDTO.getTargetId(), 
                likeDTO.getTargetType()
            );
            if (result <= 0) {
                throw new RuntimeException("取消点赞失败");
            }
            log.info("取消点赞成功，用户ID：{}，目标ID：{}，目标类型：{}", 
                    likeDTO.getUserId(), likeDTO.getTargetId(), likeDTO.getTargetType());
        } else {
            // 未点赞，添加点赞
            Like like = new Like();
            like.setUserId(likeDTO.getUserId());
            like.setTargetId(likeDTO.getTargetId());
            like.setTargetType(likeDTO.getTargetType());
            like.setCreateTime(LocalDateTime.now());
            
            int result = likeMapper.insert(like);
            if (result <= 0) {
                throw new RuntimeException("点赞失败");
            }
            log.info("点赞成功，用户ID：{}，目标ID：{}，目标类型：{}", 
                    likeDTO.getUserId(), likeDTO.getTargetId(), likeDTO.getTargetType());
        }
    }
    
    @Override
    public LikeVO getLikeInfo(Long userId, Long targetId, Integer targetType) {
        log.info("获取点赞信息，用户ID：{}，目标ID：{}，目标类型：{}", userId, targetId, targetType);
        
        if (targetId == null || targetType == null) {
            throw new RuntimeException("查询参数不能为空");
        }
        
        LikeVO likeVO = new LikeVO();
        likeVO.setTargetId(targetId);
        likeVO.setTargetType(targetType);
        
        // 获取点赞总数
        Long likeCount = likeMapper.countByTarget(targetId, targetType);
        likeVO.setLikeCount(likeCount != null ? likeCount : 0L);
        
        // 如果提供了用户ID，检查用户是否已点赞
        if (userId != null) {
            Like existingLike = likeMapper.selectByUserAndTarget(userId, targetId, targetType);
            likeVO.setIsLiked(existingLike != null);
        } else {
            likeVO.setIsLiked(false);
        }
        
        log.info("点赞信息获取成功：{}", likeVO);
        return likeVO;
    }
    
    @Override
    public Long getLikeCount(Long targetId, Integer targetType) {
        log.info("获取点赞数量，目标ID：{}，目标类型：{}", targetId, targetType);
        
        if (targetId == null || targetType == null) {
            throw new RuntimeException("查询参数不能为空");
        }
        
        Long count = likeMapper.countByTarget(targetId, targetType);
        Long result = count != null ? count : 0L;
        
        log.info("点赞数量：{}", result);
        return result;
    }
    
    @Override
    public Boolean isLiked(Long userId, Long targetId, Integer targetType) {
        log.info("检查点赞状态，用户ID：{}，目标ID：{}，目标类型：{}", userId, targetId, targetType);
        
        if (userId == null || targetId == null || targetType == null) {
            return false;
        }
        
        Like existingLike = likeMapper.selectByUserAndTarget(userId, targetId, targetType);
        Boolean result = existingLike != null;
        
        log.info("点赞状态检查结果：{}", result);
        return result;
    }
}